package com.kltn.zigo.data.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.kltn.zigo.R;
import com.kltn.zigo.data.loader.ListUserLoader;
import com.kltn.zigo.process.AccentRemover;
import com.kltn.zigo.process.ImageLoader;

public class UserAdapter extends BaseAdapter implements Filterable{
    
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private ArrayList<HashMap<String, String>> datatemp;
    private static LayoutInflater inflater=null;
    private ListUserLoader userloader;
    public ImageLoader imageLoader; 
    private ValueFilter valueFilter;
    
    public UserAdapter(Activity a, ListUserLoader pl) {
        activity = a;
        data=pl.GetUser();
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
        userloader = pl;        
    }
    
    public UserAdapter(Activity a, ListUserLoader pl, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        datatemp=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
        userloader = pl;
        getFilter();
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item_list_user, null);

        TextView user_id_zing = (TextView)vi.findViewById(R.id.tvID);
        TextView user_name = (TextView)vi.findViewById(R.id.tvUsername);
        TextView display_name = (TextView)vi.findViewById(R.id.tvDisplayname);
        TextView url_avatar = (TextView)vi.findViewById(R.id.tvUrlAvatar); 
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); 
        
        HashMap<String, String> user = new HashMap<String, String>();
        user = data.get(position);
        
        String Url_avatar = user.get(userloader.TAG_AVATAR);
        // Setting all values in listview
        user_id_zing.setText(user.get(userloader.TAG_USER_ID));
        user_name.setText(user.get(userloader.TAG_USER_NAME));
        display_name.setText(user.get(userloader.TAG_DISPLAY_NAME));
        url_avatar.setText(Url_avatar);
        imageLoader.DisplayImage(Url_avatar , thumb_image);
        
        return vi;
    }
    
    @Override
    public Filter getFilter() {

        if(valueFilter==null) {

            valueFilter=new ValueFilter();
        }

        return valueFilter;
    }
    
    private class ValueFilter extends Filter {

        //Invoked in a worker thread to filter the data according to the constraint.
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results=new FilterResults();
            
            if(constraint == null || constraint.length() == 0)
            {
                results.values = datatemp;
                results.count = datatemp.size();
            }
            else
            {
            	ArrayList<HashMap<String, String>> filterList=new ArrayList<HashMap<String, String>>();
            	//Search full
            	for(HashMap<String,String> datahashmap : datatemp)
	            {
	
	            	for (String current : datahashmap.values()) {
	            		current = AccentRemover.toRemoveVNese(current);
	            		constraint = AccentRemover.toRemoveVNese(constraint.toString());
	                    if(current.contains(constraint))	                    	
	                    	filterList.add(datahashmap);
	                }	
	
	                results.count=filterList.size();	
	                results.values=filterList;
	            }
            	
            	//Search name
            	/*HashMap<String, String> place = new HashMap<String, String>();
        		constraint = AccentRemover.toRemoveVNese(constraint.toString());
                for(int i = 0; i < data.size(); i++){
                	place = data.get(i);                
                	String nameplace = AccentRemover.toRemoveVNese(place.get(userloader.TAG_PLACE_NAME));
                	if(nameplace.contains(constraint))
                		filterList.add(place);
                }
                results.count=filterList.size();	
                results.values=filterList;*/
            }

            return results;
        }


        //Invoked in the UI thread to publish the filtering results in the user interface.
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {

            data=(ArrayList<HashMap<String, String>>) results.values;

            notifyDataSetChanged();
        }

    }

}